<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class StuController extends Controller
{
    //浏览信息
    public function index()
    {
        $data = \DB::table('stu')->get();
      
        return view('stu.index',['list'=>$data]);
    }
    
    
    //加载添加表单
    public function create()
    {
      return view("stu.add");  
    }
    
    
    //执行信息添加
    public function store(Request $request)
    {
        //获取要添加的数据
        $data = ['name'=>$request->name,'age'=>$request->age,'sex'=>$request->sex,'classid'=>$request->classid];
      
       //$m = \DB::table('stu')->insertGet($data);  //返回影响的行数
        $m = \DB::table("stu")->insertGetId($data);  //返回自增id
        if($m>0){
            return "添加成功; 自增id=".$m;
        }else {
            return "添加失败!";
        }
        //return  "执行信息添加"
    }
    
    
    //单条详细浏览
    public function show($id)
    {
        return "单条详情浏览";
    }
    
    
    //加载编辑表单
    public function edit($id)
    {
      $list = \DB::table('stu')->find($id);
      return view('stu.edit',['stu'=>$list]);
       
    }
    
    
    //执行信息编辑(修改)
    public function update(Request $request,$id)
    {
       
      $m = \DB::table('stu')->where('id',$id)->update(['name'=>$request->name,'age'=>$request->age,'sex'=>$request->sex,'classid'=>$request->classid]); 
    }
    
    
    //执行信息删除
    public function destroy(Request $request,$id)
    {
     
       $id = $request->input('id');

      $m = \DB::table('stu')->where('id','=',$id)->delete();
     
         if($m>0){
            return "删除成功！";
        }else {
            return "删除失败!";
        }
    }
}
